#include "sendreceivecontroll.h"
#include <QtGui>
#include <QtNetwork>
#include <iostream>

static const quint16 broadcastPort = 46000;
static const quint16 communicationPort = 45000;
static const quint16 initPort = 45000;

sendReceiveControll::sendReceiveControll()
{

    //senderSocket = new QUdpSocket(QO);
    //zum lauschen auf den ports
    broadcastSocket = new QUdpSocket(this);
    dataSocket = new QUdpSocket(this);
    senderSocket = new QUdpSocket(this);
    initSocket = new QUdpSocket(this);

    broadcastSocket->bind(QHostAddress::Any, broadcastPort, QUdpSocket::ShareAddress
                         | QUdpSocket::ReuseAddressHint);
    initSocket->bind(QHostAddress::Any, initPort, QUdpSocket::ShareAddress
                         | QUdpSocket::ReuseAddressHint);

    //connecte events mit funktionen
    connect(broadcastSocket, SIGNAL(readyRead()),
            this, SLOT(receivedBroadcast()));
    connect(initSocket, SIGNAL(readyRead()),
            this, SLOT(receivedData()));

}
//destruktor
sendReceiveControll::~sendReceiveControll()
{
    delete dataSocket;
    delete broadcastSocket;
    delete senderSocket;
}

// sende Broadcast
void sendReceiveControll::sendBroadcast(QByteArray datagram)
{
    senderSocket->writeDatagram(datagram.data(), datagram.size(),
                             QHostAddress::Broadcast, broadcastPort);
}

// Broadcast empfangen
void sendReceiveControll::receivedBroadcast()
{
    while (broadcastSocket->hasPendingDatagrams()) {
        datagram.resize(broadcastSocket->pendingDatagramSize());
        broadcastSocket->readDatagram(datagram.data(), datagram.size(),
                                      &senderIp, &senderPort);
        emit broadcastReadyRead(senderIp, senderPort, datagram);

    }

}

//sende Daten
void sendReceiveControll::sendData(QHostAddress targetIP, quint16 port,
                              QByteArray data)
{
    senderSocket->writeDatagram(data.data(), data.size(),
                             targetIP, port);
}

//Daten empfangen
void sendReceiveControll::receivedData()
{
    while (dataSocket->hasPendingDatagrams()) {
        datagram.resize(dataSocket->pendingDatagramSize());
        if (dataSocket->readDatagram(datagram.data(), datagram.size(),
                                    &senderIp, &senderPort) == -1)
            continue;
        else
            emit dataReadyRead(senderIp, senderPort, datagram);
    }
    while (initSocket->hasPendingDatagrams()) {
        datagram.resize(initSocket->pendingDatagramSize());
        if (initSocket->readDatagram(datagram.data(), datagram.size(),
                                &senderIp, &senderPort) == -1)
            continue;
        else
        {
            QList<QByteArray> list = datagram.split('@');
            if (list.size() != 3)
            {
                std::cout << "FEHLER bei Broadcast Ubertragung!" << std::endl;
            }
            quint16 carId = list.at(0).toInt();
            quint16 comPort = carId + communicationPort;

            dataSocket->bind(QHostAddress::Any, comPort, QUdpSocket::ShareAddress
                            | QUdpSocket::ReuseAddressHint);
            connect(dataSocket, SIGNAL(readyRead()),
                    this, SLOT(receivedData()));

            emit dataReadyRead(senderIp, senderPort, datagram);
         }
    }
}

//GETTER
QHostAddress sendReceiveControll::getSenderIP()
{
    return senderIp;
}

quint16 sendReceiveControll::getSenderPort()
{
    return senderPort;
}

QByteArray sendReceiveControll::getDatagram()
{
    return datagram;
}
